package com.xingkeduo.business.order.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.dto.AdminDetailDto;
import com.xingkeduo.dto.param.ConsumerPrivateParameterDto;
import com.xingkeduo.dto.param.ConsumerStyleParameterDto;
import com.xingkeduo.dto.param.DistanceQueryParameterDto;
import com.xingkeduo.dto.wxma.UserDto;
import com.xingkeduo.dto.wxma.WxJsApiDto;
import com.xingkeduo.dto.wxma.WxMaCodeDTO;
import com.xingkeduo.dto.wxma.WxMaUserInfo;
import com.xingkeduo.json.BaseDTOJTO;
import com.xingkeduo.remote.feign.AdminFeignService;
import com.xingkeduo.remote.feign.MessageFeignService;
import com.xingkeduo.utils.ResponseUtils;
import com.xingkeduo.utils.TokenokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
@Slf4j
public class ConsumerService {

    @Resource
    private AdminFeignService adminFeignService;
    @Resource
    private MessageFeignService messageFeignService;

    /**
     * 微信js授权接口
     *
     * @param consumerPrivateParameterDto:
     * @return : com.fasterxml.jackson.databind.node.ObjectNode
     * @author : gaozifeng
     * @date : 2019/5/30 12:00
     */
    public ObjectNode jsApi(ConsumerPrivateParameterDto consumerPrivateParameterDto) {
        if (null == consumerPrivateParameterDto.getMobile_type() || ("mobile_type").equals(consumerPrivateParameterDto.getMobile_type())) {
            log.info("trasfer  微信js授权  mobile_type 不能为空");
            return BaseDTOJTO.toErrorJson("数据不能为空");
        }
        ResponseUtils<WxJsApiDto> ticket = messageFeignService.getTicket(consumerPrivateParameterDto.getMpId(), consumerPrivateParameterDto.getCurrent_url());
        if (ticket.success()) {
            WxJsApiDto jsInfo = ticket.getData();
            ObjectNode jsonNodes = BaseDTOJTO.toWxTicket(jsInfo);
            log.info("trasfer  微信js授权  jsInfo" + jsInfo.toString());
            return jsonNodes;
        } else {
            log.info("trasfer  微信js授权  获取授权失败" + ticket.getMsg());
            return BaseDTOJTO.toErrorJson(ticket.getMsg());
        }
    }

    /**
     * 支付宝登录授权
     *
     * @param consumerPrivateParameterDto:
     * @return : com.fasterxml.jackson.databind.node.ObjectNode
     * @author : gaozifeng
     * @date : 2019/5/22 16:47
     */
    public ObjectNode alipayUser(ConsumerPrivateParameterDto consumerPrivateParameterDto) {
        try {
            ResponseUtils<WxMaUserInfo> userId = adminFeignService.aliCode(consumerPrivateParameterDto.getCode());
            log.info("traster-------------支付宝登录授权code返回的信息[{}]", userId);
            if (userId.success()) {
                WxMaUserInfo aliUserInfo = userId.getData();
                String id = aliUserInfo.getId();
                ObjectNode jsonNodes = BaseDTOJTO.toJsonSuccess();
                String time = String.valueOf(System.currentTimeMillis());
                log.info("traster--------------------支付宝登录授权cpde-----WxMaUserInfo[{}]--", JSON.toJSONString(userId));
//                String encrypt = TokenokenUtil.getInstance().encrypt(id + time);
                String encrypt = TokenokenUtil.createRandomCharData(20);
                UserDto dto = new UserDto();
                dto.setF_user_id(id);
                dto.setTime(time);
                dto.setAliUserId(aliUserInfo.getAliUserId());
                dto.setToken(encrypt);
                jsonNodes.putPOJO("content", dto);
                return jsonNodes;
            } else {
                return BaseDTOJTO.toErrorJson(userId.getMsg());
            }
        } catch (Exception e) {
            return BaseDTOJTO.toErrorJson("获取信息失败，请稍后重试！");
        }
    }

    /**
     * 微信登录授权
     *
     * @param consumerPrivateParameterDto:
     * @return : com.fasterxml.jackson.databind.node.ObjectNode
     * @author : gaozifeng
     * @date : 2019/5/22 16:47
     */
    public ObjectNode wecharCode(ConsumerPrivateParameterDto consumerPrivateParameterDto) {
        try {
            consumerPrivateParameterDto.setMpId("xingkeduozhengshi");
            consumerPrivateParameterDto.setLoginType("2");
            String wxUserInfo = adminFeignService.saveWechatCode(consumerPrivateParameterDto);
//            String responseWechat = adminFeignService.saveWechatCode(consumerPrivateParameterDto);
            log.info("-----traster--------微信授权code 返回的信息wxUserInfo[{}]--", JSON.toJSONString(wxUserInfo));
            JSONObject jsonObject = JSON.parseObject(wxUserInfo);
            if (jsonObject.getString("status").equals("1")) {
                log.info("trasfer 获取微信授权用户信息-----data[{}]-----", jsonObject.getString("data"));
                String datas = jsonObject.getString("data");
                WxMaUserInfo wxMaUserInfo = JSON.parseObject(datas, WxMaUserInfo.class);
                if (StringUtils.isEmpty(wxMaUserInfo.getId())) {
                    return BaseDTOJTO.toErrorJson("获取信息失败，请稍后重试！");
                }
                String id = wxMaUserInfo.getId();
                ObjectNode jsonNodes = BaseDTOJTO.toJsonSuccess();
                String time = String.valueOf(System.currentTimeMillis());
//                String encrypt = TokenokenUtil.getInstance().encrypt(id + time);
                String encrypt = TokenokenUtil.createRandomCharData(20);
                UserDto dto = new UserDto();
                dto.setGzhOpenId(wxMaUserInfo.getGzhOpenId() == null ? null : wxMaUserInfo.getGzhOpenId());
                dto.setMinaOpenId(wxMaUserInfo.getMinaOpenId() == null ? null : wxMaUserInfo.getMinaOpenId());
                dto.setF_user_id(id);
                dto.setTime(time);
                dto.setToken(encrypt);
                jsonNodes.putPOJO("content", dto);
                return jsonNodes;
            } else {
                return BaseDTOJTO.toErrorJson(jsonObject.getString("msg"));
            }
        } catch (Exception e) {
            return BaseDTOJTO.toErrorJson("获取信息失败，请稍后重试！");
        }
    }

    /**
     * @param consumerPrivateParameterDto:
     * @return : java.lang.String
     * @author : gaozifeng
     * @date : 2019/5/5 20:44
     */
    public ObjectNode code(ConsumerPrivateParameterDto consumerPrivateParameterDto) {
        try {
            ResponseUtils<WxMaCodeDTO> code = adminFeignService.code(consumerPrivateParameterDto.getJs_code(), consumerPrivateParameterDto.getMobile_type());
            log.info("traster--------------------code:" + code);
            ObjectNode jsonNodes = BaseDTOJTO.toJsonCode(code.getData().getSessionKey());
            return jsonNodes;
        } catch (Exception e) {
            return BaseDTOJTO.toErrorJson("获取信息失败，请稍后重试！");
        }
    }

    /**
     * 小程序拉取用户信息
     *
     * @param consumerPrivateParameterDto:
     * @return : com.fasterxml.jackson.databind.node.ObjectNode
     * @author : gaozifeng
     * @date : 2019/5/9 10:18
     */
    public ObjectNode get_user_info(ConsumerPrivateParameterDto consumerPrivateParameterDto) {
        try {
            log.info("userin-----------------" + consumerPrivateParameterDto.toString());
            ResponseUtils<AdminDetailDto> user_info = adminFeignService.get_user_info(consumerPrivateParameterDto);
            if (user_info.success()) {
                ObjectNode jsonNodes = BaseDTOJTO.toJsonSuccess();
                String time = String.valueOf(System.currentTimeMillis());
                log.info("traster--------------------user_info:" + user_info);
                String id = user_info.getData().getId();
//                String encrypt = TokenokenUtil.getInstance().encrypt(id + time);
                String encrypt = TokenokenUtil.createRandomCharData(20);
                UserDto dto = new UserDto();
                dto.setF_user_id(id);
                dto.setTime(time);
                dto.setToken(encrypt);
                jsonNodes.putPOJO("content", dto);
                return jsonNodes;
            } else {
                return BaseDTOJTO.toErrorJson(user_info.getMsg());
            }
        } catch (Exception e) {
            return BaseDTOJTO.toErrorJson("获取信息失败，请稍后重试！");
        }
    }


    /**
     * 获取用户信息
     */
    public String user(ConsumerPrivateParameterDto consumerPrivateParameterDto) {
        try {
            return adminFeignService.user(consumerPrivateParameterDto.getF_user_id(), consumerPrivateParameterDto.getTime(), consumerPrivateParameterDto.getToken(), consumerPrivateParameterDto.getMobile_type());
        } catch (Exception e) {
            return BaseDTOJTO.toErrorJson("获取信息失败，请稍后重试！").toString();
        }
    }

    /**
     * 更改绑定手机号 pantong  2019年5月15日下午2:39:41
     *
     * @param consumerPrivateParameterDto
     * @return String
     */
    public String ajax_update_mobile(ConsumerPrivateParameterDto consumerPrivateParameterDto) {
        try {
            return adminFeignService.ajax_update_mobile(consumerPrivateParameterDto.getF_user_id(), consumerPrivateParameterDto.getF_code(), consumerPrivateParameterDto.getF_mobile(), consumerPrivateParameterDto.getTime(), consumerPrivateParameterDto.getToken(), consumerPrivateParameterDto.getMobile_type());
        } catch (Exception e) {
            return BaseDTOJTO.toErrorJson("获取信息失败，请稍后重试！").toString();
        }
    }

    /**
     * 获取用户标签设置项
     *
     * @return
     */
    public String revise_set(ConsumerPrivateParameterDto consumerPrivateParameterDto) {
        try {
            return adminFeignService.revise_set(consumerPrivateParameterDto.getF_user_id(), consumerPrivateParameterDto.getTime(), consumerPrivateParameterDto.getToken(), consumerPrivateParameterDto.getMobile_type());
        } catch (Exception e) {
            return BaseDTOJTO.toErrorJson("获取信息失败，请稍后重试！").toString();
        }
    }

    /**
     * 修改手机号发送验证码
     *
     * @param ConsumerPrivateParameterDto
     * @return
     */
    public String ajax_send_sms_vertify_code(ConsumerPrivateParameterDto consumerPrivateParameterDto) {
        try {
            return adminFeignService.ajax_send_sms_vertify_code(consumerPrivateParameterDto.getF_user_id(), consumerPrivateParameterDto.getF_mobile(), consumerPrivateParameterDto.getTime(), consumerPrivateParameterDto.getToken(), consumerPrivateParameterDto.getMobile_type());
        } catch (Exception e) {
            return BaseDTOJTO.toErrorJson("发送失败，请稍后重试！").toString();
        }
    }

    /**
     * 查询work标签
     *
     * @param ConsumerPrivateParameterDto
     * @return
     */
    public String update_work(ConsumerPrivateParameterDto consumerPrivateParameterDto) {
        try {
            return adminFeignService.update_work(consumerPrivateParameterDto.getF_user_id(), consumerPrivateParameterDto.getTime(), consumerPrivateParameterDto.getToken(), consumerPrivateParameterDto.getMobile_type());
        } catch (Exception e) {
            return BaseDTOJTO.toErrorJson("获取信息失败，请稍后重试！").toString();
        }
    }

    /**
     * 修改work标签
     *
     * @param distanceQueryParameterDto
     * @return
     */
    public String ajax_update_work(ConsumerPrivateParameterDto consumerPrivateParameterDto) {
        try {
            return adminFeignService.ajax_update_work(consumerPrivateParameterDto.getF_user_id(), consumerPrivateParameterDto.getTime(), consumerPrivateParameterDto.getToken(), consumerPrivateParameterDto.getF_professional_attributes(), consumerPrivateParameterDto.getMobile_type());
        } catch (Exception e) {
            return BaseDTOJTO.toErrorJson("保存失败，请稍后重试！").toString();
        }
    }

    /**
     * 查询sex标签
     *
     * @param ConsumerPrivateParameterDto
     * @return
     */
    public String update_sex(ConsumerPrivateParameterDto consumerPrivateParameterDto) {
        try {
            return adminFeignService.update_sex(consumerPrivateParameterDto.getF_user_id(), consumerPrivateParameterDto.getTime(), consumerPrivateParameterDto.getToken(), consumerPrivateParameterDto.getMobile_type());
        } catch (Exception e) {
            return BaseDTOJTO.toErrorJson("获取信息失败，请稍后重试！").toString();
        }
    }

    /**
     * 修改sex标签
     *
     * @param distanceQueryParameterDto
     * @return
     */
    public String ajax_update_sex(ConsumerPrivateParameterDto consumerPrivateParameterDto) {
        try {
            return adminFeignService.ajax_update_sex(consumerPrivateParameterDto.getF_user_id(), consumerPrivateParameterDto.getTime(), consumerPrivateParameterDto.getToken(), consumerPrivateParameterDto.getF_sex(), consumerPrivateParameterDto.getMobile_type());
        } catch (Exception e) {
            return BaseDTOJTO.toErrorJson("保存失败，请稍后重试！").toString();
        }
    }

    /**
     * 查询age标签
     *
     * @param ConsumerPrivateParameterDto
     * @return
     */
    public String update_age(ConsumerPrivateParameterDto consumerPrivateParameterDto) {
        try {
            return adminFeignService.update_age(consumerPrivateParameterDto.getF_user_id(), consumerPrivateParameterDto.getTime(), consumerPrivateParameterDto.getToken(), consumerPrivateParameterDto.getMobile_type());
        } catch (Exception e) {
            return BaseDTOJTO.toErrorJson("获取信息失败，请稍后重试！").toString();
        }
    }

    /**
     * 修改age标签
     *
     * @param distanceQueryParameterDto
     * @return
     */
    public String ajax_update_age(ConsumerPrivateParameterDto consumerPrivateParameterDto) {
        try {
            return adminFeignService.ajax_update_age(consumerPrivateParameterDto.getF_user_id(), consumerPrivateParameterDto.getTime(), consumerPrivateParameterDto.getToken(), consumerPrivateParameterDto.getSelected_tag(), consumerPrivateParameterDto.getMobile_type());
        } catch (Exception e) {
            return BaseDTOJTO.toErrorJson("保存失败，请稍后重试！").toString();
        }
    }

    /**
     * 查询风格定位
     *
     * @param distanceQueryParameterDto
     * @return
     */
    public String update_style(ConsumerStyleParameterDto consumerStyleParameterDto) {
        try {
            return adminFeignService.update_style(consumerStyleParameterDto.getF_user_id(), consumerStyleParameterDto.getTime(), consumerStyleParameterDto.getToken(), consumerStyleParameterDto.getMobile_type());
        } catch (Exception e) {
            return BaseDTOJTO.toErrorJson("获取信息失败，请稍后重试！").toString();
        }
    }

    /**
     * 修改风格定位
     *
     * @param distanceQueryParameterDto
     * @return
     */
    public String ajax_update_style(ConsumerStyleParameterDto consumerStyleParameterDto) {
        try {
            return adminFeignService.ajax_update_style(consumerStyleParameterDto.getF_user_id(), consumerStyleParameterDto.getTime(), consumerStyleParameterDto.getToken(), consumerStyleParameterDto.getChoose_style_list_ids(), consumerStyleParameterDto.getMobile_type());
        } catch (Exception e) {
            return BaseDTOJTO.toErrorJson("保存失败，请稍后重试！").toString();
        }
    }

    /**
     * 查询到店距离
     *
     * @param distanceQueryParameterDto
     * @return
     */
    public String update_care(DistanceQueryParameterDto distanceQueryParameterDto) {
        try {
            return adminFeignService.update_care(distanceQueryParameterDto.getF_user_id(), distanceQueryParameterDto.getTime(), distanceQueryParameterDto.getToken(), distanceQueryParameterDto.getMobile_type());
        } catch (Exception e) {
            return BaseDTOJTO.toErrorJson("获取信息失败，请稍后重试！").toString();
        }
    }


    /**
     * 修改到店距离
     *
     * @param distanceQueryParameterDto
     * @return
     */
    public String ajax_update_care(DistanceQueryParameterDto distanceQueryParameterDto) {
        try {
            return adminFeignService.ajax_update_care(distanceQueryParameterDto.getF_user_id(), distanceQueryParameterDto.getTime(), distanceQueryParameterDto.getToken(), distanceQueryParameterDto.getF_frequency(), distanceQueryParameterDto.getMobile_type());
        } catch (Exception e) {
            return BaseDTOJTO.toErrorJson("保存失败，请稍后重试！").toString();
        }
    }
}
